package com.org.gsc.zsqdemo2;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity {
    private List<Fragment> mfrags=new ArrayList<>();
    private int[] mIconSelectIds = {
            R.drawable.tab_home_select, R.drawable.tab_speech_select,
            R.drawable.tab_contact_select, R.drawable.tab_more_select};
    private int[] mIconUnselectIds = {
            R.drawable.tab_home_unselect, R.drawable.tab_speech_unselect,
            R.drawable.tab_contact_unselect, R.drawable.tab_more_unselect};
    private String[] mTitles = {"首页", "消息", "联系人", "更多"};
    @Override
    protected void DoBusy() {
        mfrags.add(new FistFragmentDemo());
        mfrags.add(new SecondFragmentDemo());
        mfrags.add(new ThirdFragmentDemo());
        mfrags.add(new FourFragmentDemo());
        initviews();
    }
    CommonTabLayout msltab;
    ViewPager mvp;
    private void initviews() {
        msltab=findview(R.id.tab);
        mvp=findview(R.id.vp);
        mvp.setAdapter(new MyAdapter(getSupportFragmentManager(),mfrags));
        ArrayList<CustomTabEntity> tabs=new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            tabs.add(new DemoCustomTabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        msltab.setTabData(tabs);
        msltab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mvp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mvp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
               msltab.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }
}
